import { _decorator, Component, Node, sys } from 'cc';
import { tgxSceneUtil, tgxUIAlert, tgxUIMgr } from '../../core_tgx/tgx';
import { UILogin } from '../ui_login/UILogin';
import { SceneDef } from '../../scripts/SceneDef';
import { NetUtil } from '../scripts/NetUtil';
import { urlParse } from '../../core_tgx/base/URLUtils';
import { UserMgr } from '../scripts/UserMgr';
const { ccclass, property } = _decorator;

@ccclass('LoginScene')
export class LoginScene extends Component {
    async start() {

        if(sys.isBrowser){
            let params = urlParse();
            let account = params['a'];
            let password = params['p'];
            if(account && password){
                let ret = await UserMgr.inst.doLogin(account, password);
                if(ret.isSucc){
                    return;
                }
            }
        }
        
        tgxUIMgr.inst.hideAll();
        tgxUIMgr.inst.showUI(UILogin);

        NetUtil.addErrorFilter('INVALID_TOKEN',()=>{
            tgxUIAlert.show('TOKEN 过期，请重新登录').onClick(() => {
                tgxSceneUtil.loadScene(SceneDef.LOGIN);
            });
        });
    }

    update(deltaTime: number) {
        
    }
}